﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace DeviceStub
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool m_Listen = false;
        private void btnPowerOn_Click(object sender, EventArgs e)
        {
            if (!m_Listen)
            {
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(StartListen));
                thread.Start();
                label1.Text = "Power On";
                btnPowerOn.Text = "Power Off";
            }
            else
            {
                StopListen();
                label1.Text = "Power Off";
                btnPowerOn.Text = "Power On";
            }
            m_Listen = !m_Listen;
        }

        Socket m_Socket = null;
        public void StartListen()
        {
            m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, XSSDP.ssdp.SDDP_PORT);
            EndPoint ep = (EndPoint)iep;
            m_Socket.Bind(iep);
            m_Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse(XSSDP.ssdp.SDDP_ADDRESS)));
            byte[] buffer = new byte[1024];
            try
            {
                while (true)
                {
                    int len = m_Socket.ReceiveFrom(buffer, ref ep);
                    string ip = ((IPEndPoint)ep).Address.ToString();
                    string cmd = System.Text.Encoding.Default.GetString(buffer);
                    XSSDP.ssdp obj = XSSDP.ssdp.Parse(buffer, len);
                    if (obj.MessageType == XSSDP.MessageType.SearchRequest)
                    {
                        Console.WriteLine("SDDP Request From " + ip);
                        //if (ip != "192.168.6.150") continue;

                        XSSDP.ssdp ret = new XSSDP.ssdp();
                        ret.MessageType = XSSDP.MessageType.SearchResponse;
                        ret.Location = local_ip;
                        ret.ST = obj.ST;
                        ret.OSVersion = "windows/5.0";
                        ret.ProductVersion = "stub/1.0";
                        SendResponse(ret);
                    }
                }
            }
            catch (Exception ex)
            {
                ;
            }
        }

        public void SendResponse(XSSDP.ssdp obj)
        {
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse(XSSDP.ssdp.SDDP_ADDRESS), XSSDP.ssdp.SDDP_PORT);
            EndPoint ep = (EndPoint)iep;
            byte[] buffer = obj.GetCommandBytes();
            s.SendTo(buffer, ep);
            s.Close();
        }

        public void StopListen()
        {
            m_Socket.Close();
        }
        string local_ip = "";
        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "Power Off";
            local_ip = new System.Net.IPAddress(System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0].Address).ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}
